package com.controller;

import com.dao.ISalgradeDAO;
import com.util.SessionUtil;
import com.vo.Salgrade;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 鲁昊天
 * @date 2024/12/16
 */
@Controller
public class SalgradeController {

    ISalgradeDAO salgradeDAO = null;
    SqlSession session = null;
    @RequestMapping("selectAllSalgrade")
    public String selectAllSalgrade(Model model) throws Exception {
        SessionUtil sessionUtil = new SessionUtil(); //获取mybatis的Session对象
        session = sessionUtil.getSession();
        if (session != null) {
            salgradeDAO = session.getMapper(ISalgradeDAO.class);//装载接口类
            List<Salgrade> salgrades = salgradeDAO.selectAll(); //调用接口中 定义的数据库操作方法
            model.addAttribute("salgrades", salgrades);
            //session.commit();  如果遇到 insert delete update 必须执行commit
        }
        sessionUtil.destory(); //关闭session
        return "showSalgrade.jsp";
    }
    @RequestMapping("delSalgrade/{id}")
    public String delSalgrade(@PathVariable("id") int grade) throws Exception {
        SessionUtil sessionUtil = new SessionUtil(); //获取mybatis的Session对象
        session = sessionUtil.getSession();
        if (session != null) {
            salgradeDAO = session.getMapper(ISalgradeDAO.class);//装载接口类
            int result =  salgradeDAO.delSalgrade(grade); //调用接口中 定义的数据库操作方法
            session.commit();  //如果遇到 insert delete update 必须执行commit
            if (result == 1) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
        }
        sessionUtil.destory(); //关闭session
        return  "/selectAllSalgrade";
    }
    @RequestMapping("findSalgrade/{id}")
    public String findSalgradeById(@PathVariable("id") int grade, HttpSession session1){
        SessionUtil sessionUtil = new SessionUtil(); //获取mybatis的Session对象
        session = sessionUtil.getSession();
        if (session != null) {
            salgradeDAO = session.getMapper(ISalgradeDAO.class);//装载接口类
           Salgrade salgrade = salgradeDAO.findSalgradeById(grade); //调用接口中 定义的数据库操作方法
            session1.setAttribute("salgrades", salgrade);
            //session.commit();  如果遇到 insert delete update 必须执行commit
        }
        sessionUtil.destory(); //关闭session
        return "/Sedit.jsp";
    }
    @RequestMapping("updateSalgrade")
    public String updateSalgrade(Salgrade salgrade){
        SessionUtil sessionUtil = new SessionUtil(); //获取mybatis的Session对象
        session = sessionUtil.getSession();
        if (session != null) {
            salgradeDAO= session.getMapper(ISalgradeDAO.class);//装载接口类
            int result = salgradeDAO.updateSalgrade(salgrade); //调用接口中 定义的数据库操作方法
            session.commit();  //如果遇到 insert delete update 必须执行commit
            if (result == 1) {
                System.out.println("更新成功");
            } else {
                System.out.println("更新失败");
            }
        }
        sessionUtil.destory(); //关闭session
        return "/selectAllSalgrade";
    }
}
